﻿using LyData.Data;
using System;
using System.Data;
using System.Web;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_Article : System.Web.UI.Page
{
    protected void Add_Click(object sender, EventArgs e)
    {
        if (base.IsValid)
        {
            if (!ManagerLogin.ValidateRole(string.Format("1_{0}_c", this.ArticleClassIdList.SelectedValue)))
            {
                this.Msg.CssClass = "errorMsg";
                this.Msg.Text = "您无此权限";
                this.Msg.Visible = true;
            }
            else
            {
                string addtime = DateTime.Now.ToString();
                string Direc = "../html/" + DateTime.Now.Year + DateTime.Now.Month + "/";
                string htmladdress = Direc + addtime.Replace("-", "").Replace(" ", "").Replace(":", "").Replace("/", "") + ".html";

                string[,] includeColumns = new string[,] { 
                    { "ArticleClassId", this.ArticleClassIdList.SelectedValue.ToString(), "int", "0" }, 
                    { "Title", this.ArticleTitle.Text, "NVarChar", "0" }, 
                    { "Author", this.Author.Text, "NVarChar", "0" }, 
                    { "ArticleContent", this.ArticleContent.Text.ToString(), "NText", "0" }, 
                    { "MenuId", (string)this.Session["MenuId"], "int", "0" }, 
                    { "AddTime", this.AddTime.Text, "DateTime", "0" }, 
                    { "SiteLanguage", SiteInfo.SiteLanguage(), "int", "0" }, 
                    { "htmladdress", htmladdress, "NVarChar", "0" },
                    { "Weight", this.txtorder.Text,"int","32" },
                    { "keywords", txtkeys.Text.Trim(), "NVarChar", "0" },
                    {"PageDesc",textDesc.Text.Trim(),"NVarChar", "0"}
                };

                IDb db = new DbFactory().DriverDb();
                db.Open();
                db.Insert("Article", includeColumns);
                db.Close();
                this.Msg.Text = "文章添加成功！";
                this.Msg.Visible = true;
                this.ArticleTitle.Text = "";
                this.ArticleContent.Text = "";
                this.AddTime.Text = DateTime.Now.ToString();
            }
        }
    }

    protected void Bind()
    {
        string str = base.Request.QueryString["menuId"];
        if (str == null)
        {
            string str2 = (string)this.Session["MenuId"];
            if (str2 == null)
            {
                ManagerLogin.Out();
                return;
            }
            str = str2;
        }
        this.ArticleLink.NavigateUrl = "Article.aspx?menuId=" + str;
        IDb db = new DbFactory().DriverDb();
        db.Open();
        DataRow row = db.TopRow("MenuName,FileName,MenuId", "menu", string.Format("MenuId={0}", str), "");
        if (db.Scalar("count(ArticleClassId)", "ArticleClass", string.Format("MenuId={0}", str)) < 1)
        {
            string[,] includeColumns = new string[,] { { "ClassName", (string)row["MenuName"], "NVarChar", "0" }, { "FileName", (string)row["FileName"], "NVarChar", "32" }, { "MenuId", str, "int", "0" }, { "SiteLanguage", SiteInfo.SiteLanguage(), "int", "0" } };
            db.Insert("ArticleClass", includeColumns);
        }
        DataTable oldTable = db.DataTable("ClassName,ParentId,ArticleClassId", "ArticleClass", string.Format("MenuId={0}", str), "OrderId ASC");
        db.Close();
        oldTable.Columns.Add("ClassNameText");
        DataTable newTable = oldTable.Clone();
        Function.PopulateTable(oldTable, newTable, "ClassName", "ArticleClassId", "ParentId", 0, 0);
        this.ArticleClassIdList.DataSource = newTable;
        this.ArticleClassIdList.DataTextField = "ClassNameText";
        this.ArticleClassIdList.DataValueField = "ArticleClassId";
        this.ArticleClassIdList.DataBind();
        string str3 = base.Request.QueryString["articleClassId"];
        if (str3 != null)
        {
            this.ArticleClassIdList.SelectedValue = str3;
        }
        string str4 = (string)this.Session["penName"];
        if (str4 != null)
        {
            this.Author.Text = str4;
        }
        this.AddTime.Text = DateTime.Now.ToString();
        this.Session["MenuName"] = (string)row["MenuName"];
        this.Session["FileName"] = (string)row["FileName"];
        this.Session["MenuId"] = (string)row["MenuId"];
        this.MenuName.Text = (string)row["MenuName"];
    }

    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        try
        {
            Convert.ToDateTime(this.AddTime.Text);
            args.IsValid = true;
        }
        catch
        {
            args.IsValid = false;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        this.Msg.Visible = false;
        this.Msg.CssClass = "msg";
        if (!base.IsPostBack)
        {
            this.Bind();
        }
    }
}
